﻿namespace hedefgrup.reports.renault.definitions
{
    using hedefgrup.reports.renault;
    using hedefgrup.reports.renault.date;
    using hedefgrup.reports.renault.implementation;
    using System;
    using System.Collections.Generic;
    using System.Xml;

    public class dealerDefinition : xmlNodeBasedClass
    {
        public List<dealerDefinition> children;
        public SortedList<string, cumulativeData> cumulativeDataCache;
        public SortedList<int, cumulativeData> datas;
        private dataCache sourceData;

        public dealerDefinition(dataCache sourceData, XmlNode sourceNode) : base(sourceNode)
        {
            this.datas = new SortedList<int, cumulativeData>();
            this.cumulativeDataCache = new SortedList<string, cumulativeData>();
            this.children = new List<dealerDefinition>();
            this.sourceData = sourceData;
        }

        public string getCode()
        {
            return base.getAttributeByName("code");
        }

        public cumulativeData getCumulativeData(reportMonth dateValue)
        {
            if (!this.datas.ContainsKey(dateValue.dateValue))
            {
                this.datas.Add(dateValue.dateValue, new cumulativeData(this.sourceData));
            }
            return this.datas[dateValue.dateValue];
        }

        public cumulativeData getCumulativeData(reportMonth dateStart, reportMonth dateEnd)
        {
            if (dateStart.dateValue == dateEnd.dateValue)
            {
                return this.getCumulativeData(dateStart);
            }
            string key = dateStart.dateValue.ToString() + dateEnd.dateValue.ToString();
            if (!this.cumulativeDataCache.ContainsKey(key))
            {
                cumulativeData data = new cumulativeData(this.sourceData);
                reportMonth[] monthArray = reportMonth.getMonthsInInterval(dateStart, dateEnd);
                foreach (reportMonth month in monthArray)
                {
                    data.appendCumulativeData(this.getCumulativeData(month));
                }
                this.cumulativeDataCache.Add(key, data);
            }
            return this.cumulativeDataCache[key];
        }

        public string getName()
        {
            return base.getAttributeByName("name");
        }

        public string getSortKey()
        {
            return (this.getSortPrefix() + this.getCode());
        }

        public string getSortPrefix()
        {
            return base.getAttributeByName("sort_prefix");
        }

        public string getTypeCode()
        {
            return base.getAttributeByName("type");
        }

        public bool isParentOf(string dealerCode)
        {
            if (string.IsNullOrEmpty(dealerCode))
            {
                return true;
            }
            foreach (dealerDefinition child in this.children)
            {
                if (child.getCode() == dealerCode)
                {
                    return true;
                }
                if (child.isParentOf(dealerCode))
                {
                    return true;
                }
            }
            return false;
        }

        public bool isParentOfAny(string[] dealerCodes)
        {
            if (dealerCodes == null)
            {
                return false;
            }
            foreach (string dealerCode in dealerCodes)
            {
                if (isParentOf(dealerCode))
                {
                    return true;
                }
            }
            return false;
        }

        public override string ToString()
        {
            return (this.getTypeCode() + " - " + this.getCode());
        }
    }
}

